package com.csu.bailan.controller;

import com.csu.bailan.common.CONSTANT;
import com.csu.bailan.common.CommonResponse;
import com.csu.bailan.domain.Order;
import com.csu.bailan.domain.User;
import com.csu.bailan.dto.CreateOrderDTO;
import com.csu.bailan.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order/")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("neworder")
    public CommonResponse<String> createOrder(@RequestBody CreateOrderDTO createOrderDTO, HttpSession session) {
        User loginUser = (User) session.getAttribute(CONSTANT.LOGIN_USER);
        if(loginUser == null){
            return CommonResponse.createForError("用户未登录");
        }
        if(orderService.createOrder(createOrderDTO, loginUser.getUserId())) {
            return CommonResponse.createForSuccess();
        }
        return CommonResponse.createForError("创建订单失败");
    }

    @GetMapping("myorders")
    public CommonResponse<List> orderList(HttpSession session) {
        User loginUser = (User) session.getAttribute(CONSTANT.LOGIN_USER);
        if(loginUser == null){
            return CommonResponse.createForError("用户未登录");
        }
        return CommonResponse.createForSuccess(orderService.getOrderByUserID(loginUser.getUserId()));
    }

    @GetMapping("info")
    public CommonResponse<List> orderItemList(@RequestParam String orderId, HttpSession session) {
        User loginUser = (User) session.getAttribute(CONSTANT.LOGIN_USER);
        if(loginUser == null){
            return CommonResponse.createForError("用户未登录");
        }
        return CommonResponse.createForSuccess(orderService.getOIByOrderId(orderId));
    }
}
